package com.github.fujianlian.klinechart.indicator;

import com.github.fujianlian.klinechart.KLineEntity;
import com.github.fujianlian.klinechart.entity.MA;
import com.github.fujianlian.klinechart.entity.SKDJ;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 数据辅助类 计算macd rsi等
 * Created by tifezh on 2016/11/26.
 */
public class DataHelper {

    static List<IIndicator> indicators = new ArrayList<>();

    public static void add(IIndicator iIndicator) {
        if (indicators.contains(iIndicator))
            return;
        indicators.add(iIndicator);
    }

    public static void remove(IIndicator iIndicator) {
        indicators.remove(iIndicator);
    }

    /**
     * 计算RSI
     * <p>
     * Rmax = 今日的收盘价 - 昨日的收盘价  与0取最大值    称为涨幅
     * RAbs = 今日的收盘价 -昨日的收盘价  取绝对值       称为差值
     * <p>
     * rsiMaxEma = Rmax + （    ）    涨幅平均   0-x
     * rsiABSEma = RAbs +（  ）       差值平均  0-x
     * rsi  =   (rsiMaxEma / rsiABSEma) * 100    等于涨幅与差值的比值*100   范围 0-100
     * <p>
     * 意义： 上涨与涨幅  下跌取0 的一个移动平均涨幅与操作值的比  取值范围 0-1
     * <p>
     * 原理  上涨时 涨幅==差值  慢慢增加rsi
     * 下跌是 涨幅、差值=0  慢慢减小rsi
     * <p>
     * 基于n日的一个涨幅比做的平均值 就算出的涨幅 买卖活跃性的一个指标
     */
    static void calculateRSI(List<KLineEntity> dataList, int day) {
        Float rsi;
        float rsiABSEma = 0;
        float rsiMaxEma = 0;
        for (int i = 0; i < dataList.size(); i++) {
            KLineEntity point = dataList.get(i);
            final float closePrice = point.getClose();
            if (i == 0) {
                rsi = 0f;
                rsiABSEma = 0;
                rsiMaxEma = 0;
            } else {
                float Rmax = Math.max(0, closePrice - dataList.get(i - 1).getClose());
                float RAbs = Math.abs(closePrice - dataList.get(i - 1).getClose());

                rsiMaxEma = (Rmax + (day - 1) * rsiMaxEma) / day;
                rsiABSEma = (RAbs + (day - 1) * rsiABSEma) / day;
                rsi = (rsiMaxEma / rsiABSEma) * 100;
            }
            if (i < day - 1) {
                rsi = 0f;
            }
            if (rsi.isNaN())
                rsi = 0f;
            point.rsi = rsi;
        }
    }

    /**
     * 计算kdj
     *
     * @param dataList num 数量
     */
    static void calculateKDJ(List<KLineEntity> dataList, int num) {
        float k = 0;
        float d = 0;
        for (int i = 0; i < dataList.size(); i++) {
            KLineEntity point = dataList.get(i);
            final float closePrice = point.getClose();
            int startIndex = i - num;
            if (startIndex < 0) {
                startIndex = 0;
            }
            float max14 = Float.MIN_VALUE;
            float min14 = Float.MAX_VALUE;
            for (int index = startIndex; index <= i; index++) {
                max14 = Math.max(max14, dataList.get(index).getHigh());
                min14 = Math.min(min14, dataList.get(index).getLow());
            }
            Float rsv = 100f * (closePrice - min14) / (max14 - min14);
            if (rsv.isNaN()) {
                rsv = 0f;
            }
            if (i == 0) {
                k = 50;
                d = 50;
            } else {
                k = (rsv + 2f * k) / 3f;
                d = (k + 2f * d) / 3f;
            }
            if (i < 13) {
                point.k = 0;
                point.d = 0;
                point.j = 0;
            } else if (i == 13 || i == 14) {
                point.k = k;
                point.d = 0;
                point.j = 0;
            } else {
                point.k = k;
                point.d = d;
                point.j = 3f * k - 2 * d;
            }
        }

    }
    /**
     * 计算Skdj
     *
     * @param dataList
     *
     *  SKDJ指标的计算公式：
     *
     *       第一步：计算RSV值=（收盘价-N日内最低价）/（N日内最高价-N日内最低价）*100
     *       第二步：计算MARSV=RSV的M日指数移动平均
     *       第三步：计算K=MARSV的M日移动平均
     *       第四步：计算D=K的M日移动平均
     *       SKDJ指标的原理：
     *       KDJ指标则反应的是市场上随机波动较快的情况，而SKDJ慢速随机指标通过差值平均过滤了短期的波动，反应的是市场上随机波动较慢的情况。SKDJ指标中，代表慢速的K值其实就是KDJ指标中的D值，并且SKDJ指标中的K值就是其慢速K值的移动平均值。
     *       SKDJ指标的使用技巧：
     *       一、SKDJ慢速随机指标基础使用技巧
     *       1、SKDJ慢速随机指标也有超卖区间和超买区间的划分，当SKDJ慢速随机指标>80时，属于超买区间，回档机率大；当SKDJ慢速随机指标<20
     *       时，属于超卖区间，反弹机率大；
     *       2、当SKDJ慢速随机指标的K（白线）在20左右向上交叉D时，视为买进信号；
     *       3、当SKDJ慢速随机指标的K在80左右向下交叉D时，视为卖出信号；
     *       4、当SKDJ波动于50左右的任何讯号，其作用不大。
     *       二、SKDJ慢速随机指标进阶使用技巧
     *       1、SKDJ慢速随机指标引入头肩和多重顶底形态。关于形态方便的知识可以翻看笔者以前文章。这里强调一下：形态必须在较高位置或者较低位置出现才有效果，操作时可按形态学方面的操作原则进行即可。如下图SKDJ指标的M头形态、W底形态。
     *
     *       2、SKDJ慢速随机指标引入背离技术。简单地说,背离就是技术指标与价格走势不一致的现象。当K、D处于高位（大于80）,并形成依次向下趋势的两个或多个峰,而股票的价格走势却没有向下,这叫顶背离,是卖出信号。反之，当K、D处于低位,并形成一底比一底高,而股价却维持持续下跌趋势,构成技术指标与股价的底背离,是买入信号。如下图说是SKDJ指标的顶背离形态。
     *
     *       3、SKDJ指标在使用的过程中也存在失误的地方，这不应该归咎于指标的错，指标只是武器，关键在于使用武器的人。这里笔者提一点：股票长期运行在年线之上的技术指标有效性较高，股价被长期均线压制的情况下技术指标有效性较低。如下图所示：
     *       股价长期运行在年线之上的情况准确性较高：
     *
     *       股价长期运行在年线之下的情况准确性较低：
     */

    /**
     * 第一步：计算RSV值=（收盘价-N日内最低价）/（N日内最高价-N日内最低价）*100
     * 第二步：计算MARSV=RSV的M日指数移动平均
     * 第三步：计算K=MARSV的M日移动平均
     * 第四步：计算D=K的M日移动平均
     * <p>
     * 当前收盘价 与最低价的差值 比上  最高价 与最低价的差值
     * <p>
     * 股价上涨  当前收盘价慢慢接近最高价   值慢慢趋近于1
     * 当当前价不断新高时   价格维持在1 附近
     * 股价下跌  收盘价慢慢远离最高价    值慢慢趋近于0
     * 当当前价格低于最低价并不断创新低  值维持在0 附近
     * skdj指标 描述的就是 价格在指定日时间段内的最高价与最低价之间运行的情况 。
     */
    static void calculateSKDJ(List<KLineEntity> dataList, int M, int N) {
        for (int i = 0; i < dataList.size(); i++) {
            KLineEntity point = dataList.get(i);
            final float closePrice = point.getClose();
            int startIndex = i - N;
            if (startIndex < 0) {
                startIndex = 0;
            }
            float max14 = Float.MIN_VALUE;
            float min14 = Float.MAX_VALUE;
            for (int index = startIndex; index <= i; index++) {
                max14 = Math.max(max14, dataList.get(index).getHigh());
                min14 = Math.min(min14, dataList.get(index).getLow());
            }
            float rsv = 100f * (closePrice - min14) / (max14 - min14);
            point.setRsv(rsv);
        }

        for (int i = 0; i < dataList.size(); i++) {
            KLineEntity point = dataList.get(i);
            SKDJ skdj = new SKDJ();

            int startIndex = i - M;
            if (startIndex < 0) {
                startIndex = 0;
            }

            int count = 0;
            float rsvM = 0;
            for (int index = startIndex; index <= i; index++) {
                KLineEntity point2 = dataList.get(index);
                float rsv = point2.getRsv();
                ++count;
                rsvM += rsv;
            }
            if (count != 0) {
                skdj.setSk(rsvM / count);
            }
            count = 0;
            float sk = 0;
            for (int index = startIndex; index <= i; index++) {
                KLineEntity point2 = dataList.get(index);
                float k = point2.getSk();
                ++count;
                sk += k;
            }
            if (count != 0) {
                skdj.setSd(sk / count);
            }
            point.setSkdj(skdj);
        }
    }

    /**
     * 计算wr
     * N日内 的最高值 - 当前价格   /  最高价与最低价的差值
     */
    static void calculateWR(List<KLineEntity> dataList) {
        Float r;
        for (int i = 0; i < dataList.size(); i++) {
            KLineEntity point = dataList.get(i);
            int startIndex = i - 14;
            if (startIndex < 0) {
                startIndex = 0;
            }
            float max14 = Float.MIN_VALUE;
            float min14 = Float.MAX_VALUE;
            for (int index = startIndex; index <= i; index++) {
                max14 = Math.max(max14, dataList.get(index).getHigh());
                min14 = Math.min(min14, dataList.get(index).getLow());
            }
            if (i < 13) {
                point.r = -10;
            } else {
                r = -100 * (max14 - dataList.get(i).getClose()) / (max14 - min14);
                if (r.isNaN()) {
                    point.r = 0;
                } else {
                    point.r = r;
                }
            }
        }

    }

    /**
     * 计算macd
     */
    static void calculateMACD(List<KLineEntity> dataList) {
        float ema12 = 0;
        float ema26 = 0;
        float dif = 0;
        float dea = 0;
        float macd = 0;

        for (int i = 0; i < dataList.size(); i++) {
            KLineEntity point = dataList.get(i);
            final float closePrice = point.getClose();
            if (i == 0) {
                ema12 = closePrice;
                ema26 = closePrice;
            } else {
                // EMA（12） = 前一日EMA（12） X 11/13 + 今日收盘价 X 2/13
                ema12 = ema12 * 11f / 13f + closePrice * 2f / 13f;
                // EMA（26） = 前一日EMA（26） X 25/27 + 今日收盘价 X 2/27
                ema26 = ema26 * 25f / 27f + closePrice * 2f / 27f;
            }
            // DIF = EMA（12） - EMA（26） 。
            // 今日DEA = （前一日DEA X 8/10 + 今日DIF X 2/10）
            // 用（DIF-DEA）*2即为MACD柱状图。
            dif = ema12 - ema26;
            dea = dea * 8f / 10f + dif * 2f / 10f;
            macd = (dif - dea) * 2f;
            point.dif = dif;
            point.dea = dea;
            point.macd = macd;
        }

    }

    /**
     * 计算 BOLL 需要在计算ma之后进行
     * 价格与平均值的差 去平方再平均 再开根号
     * 、
     * 实际上玩的还是均线  与价格的关系
     */
    static void calculateBOLL(List<KLineEntity> dataList) {
        for (int i = 0; i < dataList.size(); i++) {
            KLineEntity point = dataList.get(i);
            if (i < 19) {
                point.mb = 0;
                point.up = 0;
                point.dn = 0;
            } else {
                int n = 20;
                float md = 0;
                for (int j = i - n + 1; j <= i; j++) {
                    float c = dataList.get(j).getClose();
                    float m = point.getMa().getMA20Price();
                    float value = c - m;
                    md += value * value;
                }
                md = md / (n - 1);
                md = (float) Math.sqrt(md);
                point.mb = point.getMa().getMA20Price();
                point.up = point.mb + 2f * md;
                point.dn = point.mb - 2f * md;
            }
        }

    }

    /**
     * 计算ma
     *
     * @param dataList
     */
    static void calculateMA(List<KLineEntity> dataList) {
        float ma5 = 0;
        float ma10 = 0;
        float ma20 = 0;
        float ma30 = 0;
        float ma60 = 0;
        float ma120 = 0;

        for (int i = 0; i < dataList.size(); i++) {
            KLineEntity point = dataList.get(i);
            final float closePrice = point.getClose();
            MA ma = new MA();
            point.setmMa(ma);
            ma5 += closePrice;
            ma10 += closePrice;
            ma20 += closePrice;
            ma30 += closePrice;
            ma60 += closePrice;
            ma120 += closePrice;
            if (i == 4) {
                ma.MA5Price = ma5 / 5f;
            } else if (i >= 5) {
                ma5 -= dataList.get(i - 5).getClose();
                ma.MA5Price = ma5 / 5f;
            } else {
                ma.MA5Price = 0f;
            }
            if (i == 9) {
                ma.MA10Price = ma10 / 10f;
            } else if (i >= 10) {
                ma10 -= dataList.get(i - 10).getClose();
                ma.MA10Price = ma10 / 10f;
            } else {
                ma.MA10Price = 0f;
            }
            if (i == 19) {
                ma.MA20Price = ma20 / 20f;
            } else if (i >= 20) {
                ma20 -= dataList.get(i - 20).getClose();
                ma.MA20Price = ma20 / 20f;
            } else {
                ma.MA20Price = 0f;
            }
            if (i == 29) {
                ma.MA30Price = ma30 / 30f;
            } else if (i >= 30) {
                ma30 -= dataList.get(i - 30).getClose();
                ma.MA30Price = ma30 / 30f;
            } else {
                ma.MA30Price = 0f;
            }
            if (i == 59) {
                ma.MA60Price = ma60 / 60f;
            } else if (i >= 60) {
                ma60 -= dataList.get(i - 60).getClose();
                ma.MA60Price = ma60 / 60f;
            } else {
                ma.MA60Price = 0f;
            }

            if (i == 119) {
                ma.MA120Price = ma120 / 120f;
            } else if (i >= 120) {
                ma120 -= dataList.get(i - 120).getClose();
                ma.MA120Price = ma120 / 120f;
            } else {
                ma.MA120Price = 0f;
            }

        }
    }

    /**
     * 计算MA BOLL RSI KDJ MACD CCI
     *
     * @param dataList
     */
    static int skdj_M = 9, skdj_N = 6, cci_N = 14, cci_M = 54, rsi_M = 14, ene_N = 25, ene_M1 = 6, ene_M2 = 6;

    public static void setRSIPrm(int rsi_M) {
        DataHelper.rsi_M = rsi_M;

    }

    public static void setCCIPrm(int cci_N) {
        DataHelper.cci_N = cci_N;
    }

    public static void setCCIMPrm(int cci_M) {
        DataHelper.cci_M = cci_M;
    }

    public static void setSkdjPrm(int skdj_M, int skdj_N) {
        DataHelper.skdj_M = skdj_M;
        DataHelper.skdj_N = skdj_N;
    }

    public static int getSkdj_M() {
        return skdj_M;
    }

    public static int getSkdj_N() {
        return skdj_N;
    }

    public static int getRsi_M() {
        return rsi_M;
    }

    public static int getCci_N() {
        return cci_N;
    }

    public static void setEne_N(int ene_N) {
        DataHelper.ene_N = ene_N;
    }

    public static void setEne_M1(int ene_M1) {
        DataHelper.ene_M1 = ene_M1;
    }

    public static void setEne_M2(int ene_M2) {
        DataHelper.ene_M2 = ene_M2;
    }

    static ExecutorService threadPool = Executors.newCachedThreadPool();

    static {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {

            }
        });
    }

    public static void calculate(final List<KLineEntity> dataList) {
        if (dataList == null || dataList.size() == 0) return;
        long l = System.currentTimeMillis();
        final CaculateInfo caculateInfo = new CaculateInfo();

        postRun(new Runnable() {
            @Override
            public void run() {

                synchronized (dataList) {
                    try {
                        dataList.wait(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                checkOver(caculateInfo);

            }
        }, caculateInfo);


        postRun(new Runnable() {
            @Override
            public void run() {
                calculateMACD(dataList);
                checkOver(caculateInfo);

            }
        }, caculateInfo);

        postRun(new Runnable() {
            @Override
            public void run() {
                synchronized (dataList) {
                    try {
                        dataList.wait(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                calculateBOLL(dataList);
                checkOver(caculateInfo);

            }
        }, caculateInfo);

        postRun(new Runnable() {
            @Override
            public void run() {
                calculateRSI(dataList, rsi_M);
                checkOver(caculateInfo);

            }
        }, caculateInfo);


        postRun(new Runnable() {
            @Override
            public void run() {
                calculateKDJ(dataList, 13);
                checkOver(caculateInfo);

            }
        }, caculateInfo);

        postRun(new Runnable() {
            @Override
            public void run() {
                calculateWR(dataList);
                checkOver(caculateInfo);

            }
        }, caculateInfo);


        postRun(new Runnable() {
            @Override
            public void run() {
                calculateVolumeMA(dataList);
                checkOver(caculateInfo);

            }
        }, caculateInfo);
        postRun(new Runnable() {
            @Override
            public void run() {
                calculateCCI(dataList, cci_N);
                checkOver(caculateInfo);

            }
        }, caculateInfo);

        postRun(new Runnable() {
            @Override
            public void run() {
                calculateCCI_MN(dataList, cci_M, cci_N);
                checkOver(caculateInfo);

            }
        }, caculateInfo);
        postRun(new Runnable() {
            @Override
            public void run() {
                calculateSKDJ(dataList, skdj_M, skdj_N);
                checkOver(caculateInfo);

            }
        }, caculateInfo);


        postRun(new Runnable() {
            @Override
            public void run() {
                calculateENE(dataList, ene_N, ene_M1, ene_M2);
                checkOver(caculateInfo);
            }
        }, caculateInfo);


        postRun(new Runnable() {
            @Override
            public void run() {
                calculateMA(dataList);
//                calculateMAGan(dataList,5);
                synchronized (dataList) {
                    dataList.notifyAll();
                }
                checkOver(caculateInfo);

            }
        }, caculateInfo);

        synchronized (caculateInfo) {
            try {
                caculateInfo.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            otherCaculate(dataList);
        }

//        Log.e("time", "计算指标耗时：" + (System.currentTimeMillis() - l));

    }

    private static void calculateMAGan(List<KLineEntity> dataList, int d) {
        for (int i = d; i < dataList.size(); i++) {
            float ganD = 0;
            for (int j = 0; j < d; i++) {
                KLineEntity kLineEntity = dataList.get(i - j);
                KLineEntity kLineEntity1 = dataList.get(i - j - 1);
                ganD += kLineEntity.getClose() - kLineEntity1.getClose();
            }
            // 5日的平均涨幅
            dataList.get(i).setMaGanD(ganD / d);
            dataList.get(i).setMaGanDP((dataList.get(i).getClose() - dataList.get(i - 1).getClose()) / dataList.get(i).getMaGanD());

        }


    }


    private static void postRun(Runnable runnable, CaculateInfo caculateInfo) {
        caculateInfo.addToCount();
        threadPool.submit(runnable, caculateInfo);
    }

    private static void checkOver(CaculateInfo caculateInfo) {
        caculateInfo.addOvCount();
        if (caculateInfo.isOver()) {
            synchronized (caculateInfo) {
                caculateInfo.notifyAll();
            }
        }
    }

    static class CaculateInfo {
        public int count = 0;
        public int totalCOunt = 0;

        public synchronized void addToCount() {
            ++this.totalCOunt;
        }

        public synchronized void addOvCount() {
            ++this.count;
        }


        public synchronized boolean isOver() {
            return count == totalCOunt;
        }

        @Override
        public String toString() {
            return "CaculateInfo{" +
                    "count=" + count +
                    ", totalCOunt=" + totalCOunt +
                    '}';
        }
    }

    /**
     * 计算ene
     * 日期与均线的关系
     *
     * @param dataList
     * @param ene_n
     * @param ene_m1
     * @param ene_m2
     */

    private static void calculateENE(List<KLineEntity> dataList, int ene_n, int ene_m1, int ene_m2) {
        for (int i = 0; i < dataList.size(); i++) {
            if (i < ene_n)
                continue;
            KLineEntity kLineEntity = dataList.get(i);
            float ma = calculateMA(dataList, i, ene_n);
            float UPPER = (1 + ene_m1 / 100f) * ma;
            float LOWER = (1 - ene_m2 / 100f) * ma;
            float ENE = (UPPER + LOWER) / 2f;
            kLineEntity.setENE_UPPER(UPPER);
            kLineEntity.setENE_LOWER(LOWER);
            kLineEntity.setENE(ENE);
        }
    }

    private static float calculateMA(List<KLineEntity> dataList, int i, int ene_n) {
        if (i == 0)
            return 0f;

        List<KLineEntity> kLineEntities = dataList.subList((i - ene_n) < 0 ? 0 : i - ene_n, i);

        float p = 0;
        for (KLineEntity kLineEntity : kLineEntities) {
            p += kLineEntity.getClose();
        }
        return p / ene_n;
    }

    private static void otherCaculate(final List<KLineEntity> dataList) {
        for (final IIndicator indicator : indicators) {

            try {
                indicator.calculate(dataList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    private static void calculateCCI_MN(List<KLineEntity> dataList, int cci_m, int cci_n) {
        for (int i = 0; i < dataList.size(); i++) {
            float cciN = cciDay(dataList, cci_n, i);
            float cciM = cciDay(dataList, cci_m, i);
            dataList.get(i).setMCCi(cciM);
            dataList.get(i).setNCCi(cciN);
        }

    }

    private static float getCci(List<KLineEntity> dataList, int cci_n, int i) {
        float tp = getTP(dataList.get(i), cci_n);
        float ma = getMA(dataList, i, cci_n);
        float md = getMD(ma, dataList, i, cci_n);
        return (tp - ma) / md / 0.015f;
    }

    /**
     * 计算CCI
     *
     * @param dataList
     * @param n
     */
    private static void calculateCCI(List<KLineEntity> dataList, int n) {

        for (int i = 0; i < dataList.size(); i++) {
            float cci = cciDay(dataList, n, i);
            dataList.get(i).setCci(cci);
        }
    }

    /**
     * （  当日价格与平均值的差值 ）/ n日 收盘价与均线差值的 平均值
     * <p>
     * 描述的是  当前与均线的差值 与 一段时间内与均线的差值的平直 相比
     * <p>
     * 表述的是价格与均线偏离的关系  这就是为什么下穿-100 为超卖区
     * 可以参考
     */
    private static float cciDay(List<KLineEntity> dataList, int n, int i) {
        float tp = getTP(dataList.get(i), n);
        float ma = getMA(dataList, i, n);
        float md = getMD(ma, dataList, i, n);
        if (md == 0) return 0;
        return (tp - ma) / md / 0.015f;
    }

    // n日 收盘价与均线差值的平均值  
    private static float getMD(float ma, List<KLineEntity> fundsinfos, int i, int n) {
        if (i < n + 1)
            return 0;
        List<KLineEntity> fundsinfoList = fundsinfos.subList(i - n, i + 1);
        float md = 0;
        for (KLineEntity fundsinfo : fundsinfoList) {

            md += Math.abs(ma - fundsinfo.getClose());
        }
        return md / fundsinfoList.size();
    }

    // n 日的平均值
    private static float getMA(List<KLineEntity> fundsinfos, int i, int n) {
        if (i < n + 1)
            return 0;
        List<KLineEntity> fundsinfoList = fundsinfos.subList(i - n, i + 1);
        float close = 0;
        for (KLineEntity fundsinfo : fundsinfoList) {
            try {
                close += fundsinfo.getClose();
            } catch (Exception e) {
//                e.printStackTrace();
            }
        }
        return close / fundsinfoList.size();

    }

    // 当日价格的平均值
    private static float getTP(KLineEntity fundsinfo, int n) {
        return (fundsinfo.getHigh() + fundsinfo.getLow() + fundsinfo.getClose()) / 3;
    }

    private static void calculateVolumeMA(List<KLineEntity> entries) {
        float volumeMa5 = 0;
        float volumeMa10 = 0;

        for (int i = 0; i < entries.size(); i++) {
            KLineEntity entry = entries.get(i);

            volumeMa5 += entry.getVolume();
            volumeMa10 += entry.getVolume();

            if (i == 4) {
                entry.MA5Volume = (volumeMa5 / 5f);
            } else if (i > 4) {
                volumeMa5 -= entries.get(i - 5).getVolume();
                entry.MA5Volume = volumeMa5 / 5f;
            } else {
                entry.MA5Volume = 0f;
            }

            if (i == 9) {
                entry.MA10Volume = volumeMa10 / 10f;
            } else if (i > 9) {
                volumeMa10 -= entries.get(i - 10).getVolume();
                entry.MA10Volume = volumeMa10 / 10f;
            } else {
                entry.MA10Volume = 0f;
            }
        }
    }


}
